﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GgcmsTBK
{
    public partial class coupon : System.Web.UI.Page
    {
        public MySet mySet { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            mySet = new MySet();
            mySet.logined = Tools.ConvertType((Session["logined"] ?? "").ToString(), false);
            if (Request.HttpMethod.ToLower() == "post")
            {
                string ftype = Request.Form["type"]??"";
                string pass = Request.Form["pass"]??"";
                string safekey = ConfigurationManager.AppSettings["safekey"] ?? "";
                if (ftype == "login")
                {
                    string ggcms_code = (Session["ggcms_code"] ?? "").ToString().ToLower();
                    string code = Request.Form["code"];

                    if (safekey == pass )
                    {
                        Session["logined"] = true;
                        Response.Write("{\"code\":0}");
                        Response.End();
                    }
                    else
                    {
                        Session["logined"] = false;
                        Response.Write("{\"code\":1,\"key\":\"" + ggcms_code + "\"}");
                        Response.End();
                    }
                }
                else if (ftype == "file")
                {
                    if (safekey == pass)
                    {
                        string fn = Context.Server.MapPath("/tbk/data.csv");
                        string dir = Path.GetDirectoryName(fn);
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }
                        var file = Request.Files[0];
                        file.SaveAs(fn);
                        Response.Write("{\"code\":0}");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("{\"code\":2}");
                        Response.End();
                    }
                }
            }
        }
    }
}